import { createRouter, createWebHistory } from 'vue-router'

let menus = [];
const modules = import.meta.glob("./modules/**", {eager: true});
for (const modulesKey in modules) {
  const mod = modules[modulesKey].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  menus.push(...modList);
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes:[...menus]
})

// 简单的商家登录守卫：需要 `merchant_token`
router.beforeEach((to, from, next) => {
  const isMall = to.path.startsWith('/mall')
  if (!isMall) return next()
  if (to.path === '/mall/login') return next()
  const token = localStorage.getItem('merchant_token')
  if (!token) return next('/mall/login')
  next()
})

export default router
